package com.chezhujie.www.service;

import com.chezhujie.www.domain.Czjrefund;
import com.chezhujie.www.enums.RefundStatus;
import com.chezhujie.www.persistence.CzjrefundMapper;
import com.xy.core.exception.ClassCheckException;
import com.xy.user.base.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import weixin.popular.bean.paymch.SecapiPayRefundResult;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 退款记录表
 * Created by 汤伟杰 on 2017/5/23.
 */
@Service
public class CzjrefundService extends BaseService<Czjrefund> {
    @Autowired
    private CzjrefundMapper czjrefundMapper;
    @Autowired
    private WeixininfoService weixininfoService;

    @Autowired
    public void setBaseMapper(CzjrefundMapper czjrefundMapper) {
        this.baseMapper = czjrefundMapper;
    }

    public List<Map<String,Object>> getListUserinfoId(Map<String, Object> param) {
        return czjrefundMapper.listUserinfoId(param);
    }

    public void checkEntity(Czjrefund czjrefund) throws ClassCheckException {
        if(czjrefund.getRefundId()!=null&&czjrefund.getRefundId().length()>19){
            throw new ClassCheckException("退款记录表主键不能超过19个字符");
        }
        if(czjrefund.getOrderCode()!=null&&czjrefund.getOrderCode().length()>32){
            throw new ClassCheckException("退款记录表订单号不能超过32个字符");
        }
        if(czjrefund.getUserContent()!=null&&czjrefund.getUserContent().length()>255){
            throw new ClassCheckException("退款记录表用户申请退款说明不能超过255个字符");
        }
        if(czjrefund.getBusinContent()!=null&&czjrefund.getBusinContent().length()>255){
            throw new ClassCheckException("退款记录表商家退款失败说明不能超过255个字符");
        }
    }
    public void checkListEntity(List<Czjrefund> listCzjrefund) throws ClassCheckException {

        for (Czjrefund czjrefund : listCzjrefund) {
            checkEntity(czjrefund);
        }

    }

    public Czjrefund queryByOrderCode(String orderCode) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("orderCode", orderCode);
        return czjrefundMapper.queryByOne(map);
    }

    /**
     * 退款
     * @param totalAmount 要退款金额
     * @param orderCode 订单号
     * @return
     * @throws Exception
     */
    public String refundSu(BigDecimal totalAmount,String orderCode) throws Exception {
        int r=totalAmount.compareTo(BigDecimal.ZERO);
        Czjrefund czjrefund = queryByOrderCode(orderCode);
        if(czjrefund==null){
            return  "退款记录不存在！";
//                System.out.println(LocalDateTime.getDateStr(new Date())+" "+JSONObject.toJSONString(obj));
//                return "redirect:/businorder/list?failed="+ URLEncoder.encode("退款记录不存在！","UTF-8");
        }
        SecapiPayRefundResult obj;
        if(r==1){

            obj= weixininfoService.payRefund(orderCode);
            if("FAIL".equals(obj.getReturn_code())){
                return  obj.getReturn_msg();
//                System.out.println(LocalDateTime.getDateStr(new Date())+" "+ JSONObject.toJSONString(obj));
//                return "redirect:/businorder/list?failed="+ URLEncoder.encode(obj.getReturn_msg(),"UTF-8");
            }
            if("FAIL".equals(obj.getErr_code())){
                return  obj.getErr_code_des();
//                System.out.println(LocalDateTime.getDateStr(new Date())+" "+ JSONObject.toJSONString(obj));
//                return "redirect:/businorder/list?failed="+URLEncoder.encode(obj.getErr_code_des(),"UTF-8");
            }

        }
        czjrefund.setUpdatetime(new Date());
        czjrefund.setRefundStatus(RefundStatus.SUCCESS.getIndex());


        update(czjrefund);
        return null;
    }
}
